﻿using Dabr.NET.Advertisement;
using Dabr.NET.Twitter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace Dabr.NET
{
    public static class Configuration
    {
        #region Internal variables for storing
        private static bool secure, use_embedly, use_long_url, display_debug_information;
        private static APIVersion version;
        private static AdvertisementType adtype;
        private static string cookie_key, oauth_consumer_key, oauth_secret_key, embedly_key, inmobi_api_key;
        #endregion

        #region Helper functions for constructor
        private static bool ProcessBooleanProperty(String property_name)
        {
            string reserve = WebConfigurationManager.AppSettings[property_name].ToLower();
            if (reserve == "true")
                return true;
            else if (reserve == "false")
                return false;
            else
                throw new ArgumentException("Web.config property '" + property_name + "' contains an unexpected type of value.");
        }
        #endregion

        #region Boolean Properties
        public static bool Secure { get { return secure; } }
        public static bool UseEmbedLy { get { return use_embedly; } }
        public static bool UseLongURL { get { return use_long_url; } }
        public static bool DisplayDebugInformation { get { return display_debug_information; } }
        #endregion

        #region Enum Properties
        public static APIVersion APIVersion { get { return version; } }
        public static AdvertisementType AdvertisementType { get { return adtype; } }
        #endregion

        #region String Properties
        public static string CookieEncryptionKey { get { return cookie_key; } }
        public static string OAuthConsumerKey { get { return oauth_consumer_key; } }
        public static string OAuthSecretKey { get { return oauth_secret_key; } }
        public static string EmbedLyKey { get { return embedly_key; } }
        public static string InmobiAPIKey { get { return inmobi_api_key; } }
        #endregion

        static Configuration()
        {
            // Process "Secure"
            secure = ProcessBooleanProperty("Secure");
            // Process "APIVersion"
            string temp = WebConfigurationManager.AppSettings["APIVersion"];
            switch (temp)
            {
                case "1":
                case "1.0":
                    version = APIVersion.v1;
                    break;
                case "1.1":
                    version = APIVersion.v1_1;
                    break;
                default:
                    throw new ArgumentException("Web.config property 'APIVersion' contains an unexpected type of value.");
            }
            // Process "CookieEncryptionKey"
            cookie_key = WebConfigurationManager.AppSettings["CookieEncryptionKey"];
            // Process OAuth keys
            oauth_consumer_key = WebConfigurationManager.AppSettings["OAuthConsumerKey"];
            oauth_secret_key = WebConfigurationManager.AppSettings["OAuthSecretKey"];
            // Process embed.ly properties
            use_embedly = ProcessBooleanProperty("UseEmbedLy");
            if (use_embedly)
                embedly_key = WebConfigurationManager.AppSettings["EmbedLyKey"];
            else
                embedly_key = "";
            // Process advertisement types
            temp = WebConfigurationManager.AppSettings["AdvertisementType"];
            switch (temp)
            {
                case "none":
                case "None":
                    adtype = AdvertisementType.None;
                    break;
                case "Inmobi":
                case "inmobi":
                    adtype = AdvertisementType.Inmobi;
                    break;
                case "AdSense":
                case "Adsense":
                case "adSense":
                case "adsense":
                case "Google":
                case "google":
                    adtype = AdvertisementType.AdSense;
                    break;
                default:
                    throw new ArgumentException("Web.config property 'AdvertisementType' contains an unexpected type of value.");
            }
            // Inmobi-exclusive code
            inmobi_api_key = adtype == AdvertisementType.Inmobi ?
                WebConfigurationManager.AppSettings["InmobiAPIKey"] :
                "";
            // TODO: We need AdSense-exclusive codes.
            // Process "UseLongURL"
            use_long_url = ProcessBooleanProperty("UseLongURL");
            // Process "DisplayDebugInformation"
            display_debug_information = ProcessBooleanProperty("DisplayDebugInformation");
        }
    }
}